﻿using BLL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WeChat
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        static TcpClient client;
        //文件流建立到磁盘上的读写流
        //static FileStream fs = new FileStream("E:\\abcdefg.txt", FileMode.Create);
        //buffer
        static int bufferlength = 200;
        static byte[] buffer = new byte[bufferlength];
        //网络流
        static NetworkStream ns;
        protected void Button2_Click(object sender, EventArgs e)
        {
            MenuReservationBLL mb = new MenuReservationBLL();
            TcpListener listener = new TcpListener(IPAddress.Any, 51);
            //监听对象开始监听
            listener.Start();
            while (true)
            {
                Console.WriteLine("等待连接");
                //线程会挂在这里，直到客户端发来连接请求
                client = listener.AcceptTcpClient();
                Console.WriteLine("已经连接");
                //得到从客户端传来的网络流
                ns = client.GetStream();
                //如果网络流中有数据
                //if (ns.DataAvailable)
                //{
                byte[] bytes = new byte[1024];
                int bytesRead = ns.Read(bytes, 0, bytes.Length);
                string txt = Encoding.Unicode.GetString(bytes, 0, bytesRead);
                mb.Addtxt(txt);
                Response.Write("123");
                Response.Write("<script>alert(" + txt + ");</script>");
                this.TextBox2.Text = Encoding.Unicode.GetString(bytes, 0, bytesRead);
                client.Close();
                //}
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}